// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
	"shop_id": {
		"rules": [{
			"format": "string"
		}],
		"title": "店铺id，关联shop表",
		"label": "店铺id，关联shop表"
	},
	"user_id": {
		"rules": [{
			"format": "string"
		}],
		"title": "用户id,关联uni-id-users表",
		"label": "用户id,关联uni-id-users表"
	},
	"legal_person_name": {
		"rules": [{
				"format": "string"
			},
			{
				"minLength": 2,
				"maxLength": 20,
				"errorMessage": "法人信息不合法"
			}
		],
		"title": "法人姓名",
		"label": "法人姓名"
	},
	"legal_person_card": {
		"rules": [{
				"format": "string"
			},
			{
				"minLength": 15,
				"maxLength": 18,
				"errorMessage": "法人身份证信息不合法"
			}
		],
		"title": "法人身份证号码",
		"label": "法人身份证号码"
	},
	"legal_person_card_photo_z": {
		"rules": [{
			"format": "file"
		}],
		"title": "法人身份证正面",
		"label": "法人身份证正面"
	},
	"legal_person_card_photo_f": {
		"rules": [{
			"format": "file"
		}],
		"title": "法人身份证反面",
		"label": "法人身份证反面"
	},
	"business_license_title": {
		"rules": [{
			"format": "string"
		}],
		"title": "营业执照注册名称",
		"label": "营业执照注册名称"
	},
	"business_license_number": {
		"rules": [{
			"format": "string"
		}],
		"title": "营业执照注册编号，组织机构代码号",
		"label": "营业执照注册编号，组织机构代码号"
	},
	"business_license_address": {
		"rules": [{
			"format": "string"
		}],
		"title": "营业执照注册地址",
		"label": "营业执照注册地址"
	},
	"business_license_photo": {
		"rules": [{
			"format": "file"
		}],
		"title": "营业执照照片",
		"label": "营业执照照片"
	},
	"permit_photo": {
		"rules": [{
			"format": "file"
		}],
		"title": "许可证照片",
		"label": "许可证照片"
	},
	"create_date": {
		"rules": [{
			"format": "timestamp"
		}],
		"title": "创建时间",
		"defaultValue": {
			"$env": "now"
		},
		"label": "创建时间"
	},
	"audit_status": {
		"rules": [{
			"format": "bool"
		}],
		"title": "审核状态，false代表审核未通过，true代表审核通过",
		"defaultValue": false,
		"label": "审核状态，false代表审核未通过，true代表审核通过"
	},
	"audit_text": {
		"rules": [{
			"format": "string"
		}],
		"title": "审核说明文字，失败说明原因，成功直接显示：审核通过",
		"label": "审核说明文字，失败说明原因，成功直接显示：审核通过"
	}
}

const enumConverter = {}

function filterToWhere(filter, command) {
	let where = {}
	for (let field in filter) {
		let {
			type,
			value
		} = filter[field]
		switch (type) {
			case "search":
				if (typeof value === 'string' && value.length) {
					where[field] = new RegExp(value)
				}
				break;
			case "select":
				if (value.length) {
					let selectValue = []
					for (let s of value) {
						selectValue.push(command.eq(s))
					}
					where[field] = command.or(selectValue)
				}
				break;
			case "range":
				if (value.length) {
					let gt = value[0]
					let lt = value[1]
					where[field] = command.and([command.gte(gt), command.lte(lt)])
				}
				break;
			case "date":
				if (value.length) {
					let [s, e] = value
					let startDate = new Date(s)
					let endDate = new Date(e)
					where[field] = command.and([command.gte(startDate), command.lte(endDate)])
				}
				break;
			case "timestamp":
				if (value.length) {
					let [startDate, endDate] = value
					where[field] = command.and([command.gte(startDate), command.lte(endDate)])
				}
				break;
		}
	}
	return where
}

export {
	validator,
	enumConverter,
	filterToWhere
}